﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.DataLayer;
using BaHuan.Entities;
using System.Runtime;

namespace BaHuan
{
    public partial class FrmKiemSoatLien3 : Form
    {
        private KhachHangManagement khachhangMgt = new KhachHangManagement();
        private PhieuXuatHangManagement phieuxuathangMgt = new PhieuXuatHangManagement();
        private PhieuXuatHang phieuxuathang = new PhieuXuatHang();
        private KiemSoatLien3Management kiemsoatlien3Mgt = new KiemSoatLien3Management();
        DateTimePicker dtp = new DateTimePicker();
        NhanVien nhanvien = new NhanVien();
        private KhachHang khachhang;

        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public KhachHang KhachHang
        {
            set { this.khachhang = value; }
            get { return this.khachhang; }
        }

        public PhieuXuatHang PhieuXuatHang
        {
            set { this.phieuxuathang = value; }
            get { return this.phieuxuathang; }
        }

        public FrmKiemSoatLien3()
        {
            InitializeComponent();
            listDoiTuong();
        }

        private void FrmKiemSoatLien3_Load(object sender, EventArgs e)
        {
            dgvKiemSoatLien3.AutoGenerateColumns = false;
            dtp = new DateTimePicker();
            dtp.Format = DateTimePickerFormat.Short;
            dtp.Visible = true;
            dgvKiemSoatLien3.CellBeginEdit += this.dgvKiemSoatLien3_CellBeginEdit;
            dgvKiemSoatLien3.CellEndEdit += this.dgvKiemSoatLien3_CellEndEdit;
        }
        public void listDoiTuong()
        {

            ResultObj resultObj = new ResultObj();
            resultObj = khachhangMgt.GetAllKhachHang();
            List<KhachHang> listKhachHang = (List<KhachHang>)resultObj.Result;
            cboDoiTuong.DataSource = listKhachHang;
            cboDoiTuong.DisplayMember = "Ten";
            cboDoiTuong.ValueMember = "Id";
        }
        private void btnLayDuLieu_Click(object sender, EventArgs e)
        {
            
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;
            DateTime dt = new DateTime(0001,1,1);
            List<PhieuXuatHang> lstPhieuXuatHang = (from pxh in GetAllNewPhieuXuatHang()
                                                    where pxh.KhachHang.Contains(cboDoiTuong.Text) &&
                                                      pxh.NgayThang.Date >= dtTuNgay.Date && pxh.NgayThang.Date <= dtDenNgay.Date
                                                       && pxh.KhachHang == ((KhachHang)cboDoiTuong.SelectedItem).Ten
                                                      && pxh.NgayChungTu.Date != dt
                                                    select pxh).ToList();
            dgvKiemSoatLien3.DataSource = lstPhieuXuatHang;

            for (int i = 0; i < lstPhieuXuatHang.Count; i++)
            {
                if (dgvKiemSoatLien3.Rows[i].Cells[9].Value.ToString() == lstPhieuXuatHang[i].Id.ToString())
                {
                    dgvKiemSoatLien3.Rows[i].Cells[3].Value = lstPhieuXuatHang[i].listKiemSoatLien3[0].NgayBaoSoat;
                    dgvKiemSoatLien3.Rows[i].Cells[8].Value = lstPhieuXuatHang[i].listKiemSoatLien3[0].GhiChu;
                }
            }

            for (int i = 0; i < dgvKiemSoatLien3.Rows.Count; i++)
            {
                string s = dgvKiemSoatLien3.Rows[i].Cells[3].Value.ToString();
                string s1 = dgvKiemSoatLien3.Rows[i].Cells[2].Value.ToString();
                if (s == "1/1/0001 12:00:00 AM" || s == "1/1/0001 00:00:00")
                    dgvKiemSoatLien3.Rows[i].Cells[3].Value = null;
                else 
                    dgvKiemSoatLien3.Rows[i].ReadOnly = true;
            }           
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public List<PhieuXuatHang> GetAllNewPhieuXuatHang()
        {
            ResultObj resultObj = new ResultObj();

            List<PhieuXuatHang> lstPhieuXuatHang = new List<PhieuXuatHang>();
            resultObj = phieuxuathangMgt.GetAllPhieuXuatHangLien3();
            lstPhieuXuatHang = (List<PhieuXuatHang>)resultObj.Result;
            return lstPhieuXuatHang;
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            ResultObj resultObj = new ResultObj();

            if (dgvKiemSoatLien3.CurrentRow == null)
            {
                MessageBox.Show("Chưa có dữ liệu");
                return;
            }

            for (int j = 0; j < dgvKiemSoatLien3.Rows.Count; j++)
            {
                PhieuXuatHang phieuxuathang = (PhieuXuatHang)dgvKiemSoatLien3.Rows[j].DataBoundItem;
                if (!IsInsertKiemSoatLienba(phieuxuathang) && dgvKiemSoatLien3.Rows[j].Cells[3].Value != null)
                {
                    KiemSoatLien3 kiemsoatlien3 = new KiemSoatLien3();
                    kiemsoatlien3.IdPhieuXuatHang = phieuxuathang.Id;
                    kiemsoatlien3.NgayBaoSoat = Convert.ToDateTime(dgvKiemSoatLien3.Rows[j].Cells[3].Value.ToString());
                    kiemsoatlien3.GhiChu = (dgvKiemSoatLien3.Rows[j].Cells[8].Value == null ? "" : dgvKiemSoatLien3.Rows[j].Cells[8].Value.ToString());
                    kiemsoatlien3.NguoiThucHien = nhanvien.HoTen;
                    resultObj = kiemsoatlien3Mgt.SaveNewKiemSoatLien3(kiemsoatlien3);
                }
            }

            if (resultObj.ReturnCode > 0)
            {
                MessageBox.Show("Ghi thành công", "Thông báo");
            }
            else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
            else
            {
                MessageBox.Show("Chưa có dòng dữ liệu để ghi");
            }
        }

        public void BindingToGrid()
       {
            ResultObj result = new ResultObj();
            result = phieuxuathangMgt.GetAllPhieuXuatHang();
            List<PhieuXuatHang> lstPhieuXuatHang = (List<PhieuXuatHang>)result.Result;

            dgvKiemSoatLien3.DataSource = lstPhieuXuatHang;
        }

        private void dgvKiemSoatLien3_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            try
            {
                for (int i = 0; i < dgvKiemSoatLien3.Rows.Count; i++)
                {
                    if (dgvKiemSoatLien3.Rows[i].Cells["NgayBaoSoat"].Value.ToString() == "")
                    {
                        dtp.Location = dgvKiemSoatLien3.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location;
                        dtp.Visible = true;
                        if (dgvKiemSoatLien3.CurrentCell.Value != DBNull.Value)
                        {
                            dtp.Value = (DateTime)dgvKiemSoatLien3.CurrentCell.Value;
                        }
                        else
                        {
                            dtp.Value = DateTime.Today;
                        }
                    }
                    else
                    {
                        dgvKiemSoatLien3.Rows[i].Cells["NgayBaoSoat"].ReadOnly = true;
                    }
                }

            }
            catch { }
        }

        private void dgvKiemSoatLien3_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //try
            //{
            //    if (dgvKiemSoatLien3.Focused && (dgvKiemSoatLien3.CurrentCell.ColumnIndex == 3))
            //    {
            //        dgvKiemSoatLien3.CurrentCell.Value = dtp.Value.Date;
            //    }

            //}
            //catch { }
        }

        public bool IsInsertKiemSoatLienba(PhieuXuatHang phieuxuathang)
        {
            ResultObj resultObj = new ResultObj();
            resultObj = kiemsoatlien3Mgt.GetAllKiemSoatLien3();
            List<KiemSoatLien3> listKiemSoatLien3 = (from ks in (List<KiemSoatLien3>)resultObj.Result
                                                     where ks.IdPhieuXuatHang == phieuxuathang.Id
                                                     select ks).ToList();
            if (listKiemSoatLien3.Count > 0)
                return true;
            return false;
        }

        private void dgvKiemSoatLien3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
          
        }

       
    }
}

